// src/controllers/authController.js
const { authService } = require('../services/authService');
const { logger } = require('../utils/logger');

/**
 * 用户注册
 * @param {Object} req - 请求对象
 * @param {Object} res - 响应对象
 * @param {Function} next - 下一步中间件
 */
exports.register = async (req, res, next) => {
  try {
    const { username, email, password } = req.body;
    const result = await authService.register({ username, email, password });
    logger.info(`User registered: ${username}`);
    res.status(201).json(result);
  } catch (err) {
    logger.error('Registration error:', err);
    next(err);
  }
};

/**
 * 用户登录
 * @param {Object} req - 请求对象
 * @param {Object} res - 响应对象
 * @param {Function} next - 下一步中间件
 */
exports.login = async (req, res, next) => {
  try {
    const { username, password } = req.body;
    const result = await authService.login({ username, password });
    logger.info(`User logged in: ${username}`);
    res.status(200).json(result);
  } catch (err) {
    logger.error('Login error:', err);
    next(err);
  }
};